﻿using System;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Modules.CONTACT.Components;

namespace ASPNETVN.PORTAL.Modules.CONTACT
{
    public partial class AddEditItem : AddEditItemPage
    {
        protected override void OnInit(EventArgs e)
        {
            ButtonCancel = this.btnCancel;
            ButtonUpdate = this.btnUpdate;
            ButtonDelete = this.btnDelete;
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
                if (this.ItemID > 0)
                {
                    Item contact = Item.Load(this.ItemID);
                    {
                        if (contact != null)
                        {
                            txtName.Text = contact.Name;
                            txtRole.Text = contact.Role;
                            txtEmail.Text = contact.Email;
                            txtContact1.Text = contact.Contact1;
                            txtContact2.Text = contact.Contact2;
                            txtFax.Text = contact.Fax;
                            txtAddress.Text = contact.Address;
                            lblCreatedBy.Text = contact.CreatedBy;
                            lblCreationTime.Text = contact.CreationTime.ToString();
                        }
                    }
                }
                else
                {
                    lblCreatedBy.Text = PortalSecurity.CurrentUser.Identity.Email;
                    lblCreationTime.Text = DateTime.Now.ToString();
                }
            }
        }
        protected override void OnUpdate(EventArgs e)
        {
            base.OnUpdate(e);
            if (Page.IsValid)
            {
                Item contact = new Item();
                contact.ModuleID = this.ModuleID;
                contact.Name = txtName.Text;
                contact.Role = txtRole.Text;
                contact.Email = txtEmail.Text;
                contact.Contact1 = txtContact1.Text;
                contact.Contact2 = txtContact2.Text;
                contact.Fax = txtFax.Text;
                contact.Address = txtAddress.Text;

                if (this.ItemID == 0)
                {
                    contact.CreatedBy = PortalSecurity.CurrentUser.Identity.Email;
                    contact.CreationTime = DateTime.Now;
                    contact.Insert();
                }
                else
                {
                    contact.ID = this.ItemID;
                    contact.CreatedBy = lblCreatedBy.Text;
                    contact.CreationTime = DateTime.Parse(lblCreationTime.Text);
                    contact.Update();
                }

                ReturnToReferrerPage();
            }
        }

        protected override void OnDelete(EventArgs e)
        {
            base.OnDelete(e);
            if (this.ItemID != 0)
            {
                Item.Delete(this.ItemID);
            }

            ReturnToReferrerPage();
        }     
    }
}